import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Data, Params} from '@angular/router';
import {Product, ProductService} from '../share/product.service';
import {Starts} from '../stars/stars.component';

@Component({
  selector: 'app-productdetail',
  templateUrl: './productdetail.component.html',
  styleUrls: ['./productdetail.component.css']
})
export class ProductdetailComponent implements OnInit {

  private product: Product;
  /**
   * 用户输入的自定义的星级数量
   * @type {number}
   */
  private customStartCount: number = 0;
  /**
   * 星级组件 返回的星级随机数
   * @type {number}
   */
  private lastStartCount: number = 0;

  /**
   * 订阅星级组件的返回值
   */
   private starts: Starts = new Starts(0);

  constructor(private routeInfo: ActivatedRoute, private productService: ProductService) {
  }

  ngOnInit() {
    /**
     * 参数快照
     */
    /*this.productId = this.routeInfo.snapshot.queryParams["id"];*/
    const productId = this.routeInfo.snapshot.params['id'];

    this.product = this.productService.getProduct(productId);

    /**
     * 参数订阅 rxjs
     */
    /* this.routeInfo.params.subscribe((params: Params) => this.productId = params['id']);
     this.routeInfo.data.subscribe( (data: {productdetail: Productdetail}) => {
         this.productId = data.productdetail.id;
         this.productName = data.productdetail.name;
     });*/
  }

  doOnClick(event: any) {
    console.log(event);
  }

  lastStarthandler(event: Starts) {
    this.starts = event;
  }

  buyhandler(event: Starts) {
    this.starts = event;
  }
}

/**
 * 商品参数对象
 */
export class Productdetail {
  constructor(public id: number, public name: string) {

  }
}

